package com.market.cash.ui.activity

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.view.Gravity
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.market.cash.R
import com.market.cash.databinding.CashDataSafetyBinding
import com.market.cash.pro.ProActivity
import com.market.cash.utils.event.CashRootEventType
import com.market.cash.utils.helper.CashRootCustomConfigsHelper


/**
 * CashRoot
 * @Author wwdeng
 * @CreateTime: 2024/10/18 10:17
 * @Annotation：WebView
 */
class WebViewActivity : ProActivity() {
    private lateinit var binding: CashDataSafetyBinding

    companion object {
        private const val INTENT_LINK_URL = "linkUrl"
        private const val INTENT_LINK_TITLE = "linkTitle"
        fun start(context: Context, linkTitle: String, linkUrl: String) {
            val intent = Intent(context, WebViewActivity::class.java)
            intent.putExtra(INTENT_LINK_URL, linkUrl)
            intent.putExtra(INTENT_LINK_TITLE, linkTitle)
            context.startActivity(intent)
        }
    }

    override fun getCashRootRootView(): View {
        binding = CashDataSafetyBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initCashRootView() {
        setCashRootTitleBarVisibility(View.VISIBLE)
        setCashRootTitleBarColor(Color.parseColor("#FFFFFFFF"))
        setCashRootTextTitle(getLinkTitle())
        setCashRootTextTitleGravity(Gravity.CENTER)
        setCashRootRightBtnRes(R.mipmap.cash_kefu_black)

        initWebViewSettings()
    }

    override fun initCashRootListener() {
        //联系客服
        rootViewBinding.imgKefu.setOnClickListener {
            KefuActivity.start(this,"${getLinkTitle()} Page")
            addCashRootEventPoint(CashRootEventType.CLICK.CASHROOT_CLICK_CUSTOMER,"${getLinkTitle()} Page")
        }
    }

    override fun initCashRootData() {
        binding.ppWebView.loadUrl(getLinkUrl())
    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun initWebViewSettings() {
        binding.ppWebView.setInitialScale(100)
        binding.ppWebView.webChromeClient = mWebChromeClient
        binding.ppWebView.webViewClient = mWebClient
        val webSettings = binding.ppWebView.settings
        webSettings.setSupportZoom(true)
        try {
            webSettings.javaScriptEnabled = true
            webSettings.textZoom = 100
            webSettings.domStorageEnabled = true
            webSettings.builtInZoomControls = true
            webSettings.displayZoomControls = true
            webSettings.useWideViewPort = true
            webSettings.loadWithOverviewMode = true
            webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
            webSettings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
            webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
            webSettings.allowUniversalAccessFromFileURLs = true
            webSettings.allowFileAccessFromFileURLs = true
            webSettings.allowContentAccess = true
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private val mWebChromeClient = object : WebChromeClient() {
        override fun onProgressChanged(view: WebView?, newProgress: Int) {
            super.onProgressChanged(view, newProgress)

            binding.progressBar.progress = newProgress
            if (newProgress == 100) {
                binding.progressBar.visibility = View.GONE
            } else {
                binding.progressBar.visibility = View.VISIBLE
            }
        }
    }

    private val mWebClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(
            view: WebView?,
            request: WebResourceRequest?,
        ): Boolean {
            return shouldOverrideUrlLoading(view, request!!.url.toString())
        }

        @Deprecated("Deprecated in Java", ReplaceWith("false"))
        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
            return false
        }
    }

    private fun getLinkUrl(): String {
        if (intent.extras == null){
            val customData = CashRootCustomConfigsHelper.getCashRootCustomData()
            if (customData != null) {
                return customData.optString("privacyPolicyLink")
            }
        }
        return intent.extras!!.getString(INTENT_LINK_URL).toString()
    }

    fun getLinkTitle(): String {
        return intent.extras!!.getString(INTENT_LINK_TITLE).toString()
    }

    override fun onStart() {
        super.onStart()
        addCashRootEventPoint(CashRootEventType.PAGE.CASHROOT_PAGE_WEBVIEW_LINK_START,"${getLinkTitle()} Page")
    }

    override fun onDestroy() {
        super.onDestroy()
        addCashRootEventPoint(CashRootEventType.PAGE.CASHROOT_PAGE_WEBVIEW_LINK_END,"${getLinkTitle()} Page")
    }
}